package by.averianov.discounta.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;

import org.hibernate.jpa.criteria.OrderImpl;
import org.springframework.stereotype.Repository;

import by.averianov.discounta.dataaccess.UserProfileDao;
import by.averianov.discounta.datamodel.UserProfile;

@Repository
public class UserProfileDaoImpl extends AbstractDaoImpl<Long, UserProfile> implements UserProfileDao {

    protected UserProfileDaoImpl() {
        super(UserProfile.class);
    }
    
    public List<UserProfile> getAllUser(SingularAttribute<UserProfile, ?> attr, boolean ascending, int startRecord, int pageSize) {
    	
    	CriteriaBuilder cb = getEm().getCriteriaBuilder();

		CriteriaQuery<UserProfile> criteria = cb.createQuery(UserProfile.class);
		Root<UserProfile> root = criteria.from(UserProfile.class);

		criteria.select(root);
		criteria.orderBy(new OrderImpl(root.get(attr), ascending));
		TypedQuery<UserProfile> query = getEm().createQuery(criteria);
		query.setFirstResult(startRecord);
		query.setMaxResults(pageSize);

		List<UserProfile> results = query.getResultList();
		return results;
    }
    
   /* @Override
	public Long getCount() {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();

		CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
		Root<UserProfile> root = criteria.from(UserProfile.class);

		criteria.select(cb.count(root));

		TypedQuery<Long> query = getEm().createQuery(criteria);
		return query.getSingleResult();
	}*/
}
